package main

import (
	"fmt"
	"log"
	"net"
	"time"
)

func ProcessCloseWait(conn net.Conn) {
	//一定要注意关闭conn，否则会出现大佬CLOSE_WAIT连接占用系统资源
	defer conn.Close()
	for {
		buf := make([]byte, 1024)
		n, err := conn.Read(buf)
		if err != nil {
			log.Println(err.Error())
			return
		}
		fmt.Println(currentTime(), "读取到数据:", string(buf[:n]))
	}
}

func currentTime() string {
	return time.Now().Format("2006-01-02 15:04:05.000")
}

func main() {
	listen, err := net.Listen("tcp", ":8888")
	if err != nil {
		log.Fatal(err)
		return
	}
	defer listen.Close()

	for {
		conn, err := listen.Accept()
		if err != nil {
			log.Fatal(err)
			continue
		}
		go ProcessCloseWait(conn)
	}
}
